<?php


namespace Addons\SiteStat;

use Common\Controller\Addon;

/**
 * 系统环境信息插件
 * @author ezhu
 */
class SiteStatAddon extends Addon{

  public $info = array(
    'name' => 'SiteStat',
    'title' => '站点统计信息',
    'description' => '统计站点的基础信息',
    'status' => 1,
    'author' => 'ezhu',
    'version' => '1.0'
  );

  public function install(){
    return true;
  }

  public function uninstall(){
    return true;
  }

  //实现的AdminIndex钩子方法
  public function adminIndex($param){
    $config = $this->getConfig();
    $this->assign('addons_config', $config);
    if($config['display']){
      if(IS_SUPPLIER){
        $order_map = array(
          'status' => 1,
          'payment_time' => array('gt', 0),
          'supplier_ids' => UID,
        );
        $data = array(
          'info' => array(
            'order' => M('Order')->where($order_map)->count(),
          ),
          'todolist' => array(
            'ship' => M('Order')->where($order_map)->where('`o_status` = 200')->count(),
          )
        );
      }else{
        // 统计商品、订单、用户、粉丝
        $data['info'] = array(
          'user' => M('User')->join('__MEMBER__ ON __USER__.id=__MEMBER__.uid AND __USER__.status>=0')->count(),
          'item' => M('Item')->where('status >= 0')->count(),
          'order' => M('Order')->where('status >= 1')->count(),
         // 'wechat_user' => M('WechatUser')->count(),
          'shop' => M('Shop')->count(),
          'store' => M('Store')->where(array('status'=>array('egt',0)))->count(),
        );
        // 即将到期（15天之内的）
        $wMap['end_time'] = array(array('gt',time()),array('lt',time()+1296000),'and');
        $wMap['status'] = array('egt',0);

        // 统计待办事项
        $data['todolist'] = array(
          //'unrefund' => M('Order')->where('o_status = 300 AND status = 1')->count(),
          'refund' => M('Refund')->where('seller_status = 2 AND refund_status = 2')->count(),
          'withdraw' => M('Withdraw')->where('status = 100')->count(),
          'store_bill' => M('StoreCash')->where(array('status'=>2,'type'=>'apply'))->count(),
          'joinin' => M('StoreApply')->where(array('status'=>'1'))->count(),
          'pay_check' => M('StoreApply')->where(array('status'=>'4'))->count(),
          'expire' => M('Store')->where(array('end_time'=>array('elt',time()),'status'=>array('egt',0)))->count(),
          'will_expire' => M('Store')->where($wMap)->count(),
          'brand_check'=>M('Brand')->where(array('status=0'))->count(),
     //   'store_cash'=> M('StoreCash')->where(array('status'=>2,'type'=>'apply'))->count(),
          'store_edit'=>M('Store_edit_address')->where('status=0')->count(),
          'shop'=>M('Shop')->where('status=0')->count(),
        );
      }

      $menus = R('Admin/Admin/getMenus');
      
      //移除0待办
      foreach($data['todolist'] as $k => $v){
        if(0 == $v){
          unset($data['todolist'][$k]);
        }
      }
      $this->assign('data', $data);
      $this->display('info');
    }
  }

}
